package com.example.job.controller;

import com.example.job.bean.JobDetails;
import com.example.job.handler.QuartzManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/job")
public class JobController {
    @Autowired
    private QuartzManager quartzManager;

    @SuppressWarnings("unchecked")
    private static Class<? extends QuartzJobBean> getClass(String classname) throws Exception {
        Class<?> class1 = Class.forName(classname);
        return (Class<? extends QuartzJobBean>) class1;
    }

    @PostMapping(value = "/addJob")
    public void addJob(@RequestParam(value = "jobName") String jobName,
                       @RequestParam(value = "jobGroupName")String jobGroupName,
                       @RequestParam(value = "jobTime")String  jobTime) throws Exception {
        quartzManager.addOrUpdateJob(getClass(jobName),jobName,jobGroupName,jobTime);
    }
}
